package com.freeseed.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.freeseed.model.entity.Assessment;
import com.freeseed.model.mapper.AssessmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  Assessment Controller
 * </p>
 *
 * @author Allwayz
 * @since 2020-03-20
 */
@Slf4j
@RestController
@RequestMapping("assessments")
public class AssessmentController {
    @Autowired
    private AssessmentMapper assessmentMapper;

    /**
     * Get All assessments from database
     * 1. resource: assessments
     * 2. method: GET
     * @return content-type=application/json, 200
     */
    @GetMapping
    public List<Assessment> getAssessmentList(){
        List<Assessment> assessmentList = assessmentMapper.selectList(new QueryWrapper<>());
        return assessmentList;
    }

    /**
     * Get one assessments by Id from database
     * 1. resource: assessments/{Id}
     * 2. method: GET
     * @param Id
     * @return content-type=application/json, 200
     */
    @GetMapping("{Id}")
    public Assessment getAssessmentById(@PathVariable int Id){
        Assessment assessment = assessmentMapper.selectById(Id);
        return assessment;
    }

    /**
     * Insert one assessment into database
     * method: POST
     * @param assessment
     * @return content-type=application/json, 201
     */
    @PostMapping
    public Assessment addAssessment(Assessment assessment){
        assessmentMapper.insert(assessment);
        return assessment;
    }

    /**
     * Update one assessment by Id
     * method: PUT
     * @param assessment
     * @return
     */
    @PutMapping
    public Assessment updateAssessment(Assessment assessment){
        assessmentMapper.update(assessment,new UpdateWrapper<Assessment>().eq("assessment_id",assessment.getAssessmentId()));
        return assessment;
    }

    @DeleteMapping("{Id}")
    public void deleteAssessment(@PathVariable int Id){
        assessmentMapper.deleteById(Id);
    }
}
